package a66weding.com.jiehuntong.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	/**
	 * 用于保存中文的月份
	 */
	private final static String CHINESE_NUMBER[] = { "正", "二", "三", "四", "五",
			"六", "七", "八", "九", "十", "十一", "腊" };


	/**
     * 通过年份和月份 得到当月的日子
     * 
     * @param year
     * @param month
     * @return
     */
    public static int getMonthDays(int year, int month) {
		month ++;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: 
		    return 31;
		case 4:
		case 6:
		case 9:
		case 11: 
		    return 30;
		case 2:
			if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
				return 29;
			}else{
				return 28;
			}
		default:
			return  -1;
		}
    }

    /**
     * 返回当前月份1号位于周几
     * @param year
     * 		年份
     * @param month
     * 		月份，传入系统获取的，不需要正常的
     * @return
     * 	日：1		一：2		二：3		三：4		四：5		五：6		六：7
     */
    public static int getFirstDayWeek(int year, int month){
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(year, month, 1);
    	return calendar.get(Calendar.DAY_OF_WEEK);
    }
    
    /**
     * 根据列明获取周
     * @param column
     * @return
     */
    public static String getWeekName(int column){
    	switch(column){
    	case 0:
    		return "周日";
    	case 1:
    		return "周一";
    	case 2:
    		return "周二";
    	case 3:
    		return "周三";
    	case 4:
    		return "周四";
    	case 5:
    		return "周五";
    	case 6:
    		return "周六";
    	default :
    			return "";
    	}
    }
    /**
     * 根据列明获取周
     * @param column
     * @return
     */
    public static String getWeekName2(int column){
    	switch(column){
    	case 0:
    		return "星期日";
    	case 1:
    		return "星期一";
    	case 2:
    		return "星期二";
    	case 3:
    		return "星期三";
    	case 4:
    		return "星期四";
    	case 5:
    		return "星期五";
    	case 6:
    		return "星期六";
    	default :
    			return "";
    	}
    }

	/**
	 * 计算两个日期之间相差的天数
	 * @param smdate 较小的时间
	 * @param bdate  较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate)
	{

		try {

			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			smdate=sdf.parse(sdf.format(smdate));
			bdate=sdf.parse(sdf.format(bdate));
			Calendar cal = Calendar.getInstance();
			cal.setTime(smdate);
			long time1 = cal.getTimeInMillis();
			cal.setTime(bdate);
			long time2 = cal.getTimeInMillis();
			long between_days=(time2-time1)/(1000*3600*24);
			return Integer.parseInt(String.valueOf(between_days));

		}catch (Exception e){
			e.printStackTrace();
		}

		return 0;
	}

	/**
	 *字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate,String bdate) {

		try{
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(smdate));
			long time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(bdate));
			long time2 = cal.getTimeInMillis();
			long between_days=(time2-time1)/(1000*3600*24);

			return Integer.parseInt(String.valueOf(between_days));
		}catch (Exception e){
			e.printStackTrace();
		}
		return 0;
	}

	public static String getLunMonth(int month){
		return CHINESE_NUMBER[month-1];
	}

}
